<?php
/*
 * 系统配置
 */
  class SystemAction extends CommonAction{
	
    //配置列表
    public function index(){
        $m = M('system');
        if($syskey = I('get.syskey')){$map['_string'] = 'concat(sysname,syskey,sysvalue) like "%'.$syskey.'%"';}
        $count = $m->where($map)->count();
        $p = new Page($count,20);
        $list = $m->where($map)->limit("$p->firstRow,$p->listRows")->select();
        $this->assign('list',$list);
        $this->assign('fpage',$p->show());
        $this->display();
    }
    
    //配置保存
    public function save(){
        $m = D('System');
        if($id = I('get.id')){$list = $m->find($id);$this->assign('list',$list);}
        if(IS_POST){
            $_POST["syskey"] = strtoupper($_POST["syskey"]);
            if(false === $m->create()){$this->error($m->getError());} 
            if($_POST['id']){$type='save';}else{$type='add';}
            if(false === $aid = $m->$type()){$this->error('添加数据失败');}
            if($type=='save'){$aid=$id;}
            $system = $m->field('syskey,sysvalue')->select();
            foreach($system as $key=>$value){
                $config[$value['syskey']] = $value['sysvalue'];
            }
            F('system',$config,CONF_PATH);
            $this->success('保存数据成功',U('System/save',array('id'=>$aid)));exit();
        }
        $this->display();
    }
    
    //配置删除
    public function del($id){
        $m = M('system');
        if(false === $m->delete($id)){$this->error('删除配置失败');}
        $this->success('删除配置成功');
    }

		
  }